package com.erpx.api.stock;

import com.erpx.api.CommonConstants;
import com.erpx.api.WdtClient;
import com.erpx.api.param.StockInOrderDetailParam;
import com.erpx.api.param.StockOutOrderDetailVO;
import com.erpx.utils.JsonUtils;

import java.io.IOException;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class StockOrderService {

    /**
     * 入库单查询
     */
    public static void queryStockinOrderPage() {
        WdtClient client = new WdtClient(CommonConstants.sid, CommonConstants.appkey, CommonConstants.appSecret, CommonConstants.baseUrl);

        Map<String, String> params = new HashMap<String, String>();
        params.put("start_time", "2025-01-01 00:00:00");
        params.put("end_time", "2025-02-26 23:59:59");
        params.put("page_no", "0");
        params.put("page_size", "10");
        params.put("src_order_type", "1");
        params.put("src_order_no", "CG2502250402");
//        params.put("src_order_type", "6");
//        params.put("status", "10");
        try {
            String response = client.execute("stockin_order_query.php", params);
            System.out.println(response);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    /**
     * 出库单查询
     */
    public static void queryStockOutOrderPage() {
        WdtClient client = new WdtClient(CommonConstants.sid, CommonConstants.appkey, CommonConstants.appSecret, CommonConstants.baseUrl);

        Map<String, String> params = new HashMap<String, String>();
//        params.put("start_time", "2025-02-11 00:00:00");
//        params.put("end_time", "2025-02-13 23:59:59");
        params.put("page_no", "0");
        params.put("page_size", "666");
        params.put("src_order_type", "3");
//        params.put("src_order_no", "CT2502260001");
//        params.put("order_type", "2");
//        params.put("status", "20");
//        params.put("stockout_no", "CKD2502120002");
        try {
            String response = client.execute("stockout_order_query.php", params);
            System.out.println(response);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 创建其他入库单
     */
    public static void createStockinOrder() throws Exception {
        WdtClient client = new WdtClient(CommonConstants.sid, CommonConstants.appkey, CommonConstants.appSecret, CommonConstants.baseUrl);

        Map<String, String> params = new HashMap<String, String>();
        params.put("warehouse_id", "22");
        params.put("src_order_type", "6");
        params.put("remark", "桀桀桀桀");
        params.put("reason", "测试采购入库");
        params.put("is_save", "false");
        params.put("is_split_detail", "true");
        params.put("need_fail_result", "true");
        params.put("post_amount", "100");
        params.put("post_share_type", "1");

//        params.put("stock_in_order_detail_list",
//                JsonUtils.serialize(List.of(new StockInOrderDetailParam("子件001", 1, BigDecimal.valueOf(10), BigDecimal.valueOf(10), BigDecimal.valueOf(10), BigDecimal.valueOf(100), "z", "A-02-2", "HAZS1321", "2025-02-14 11:00:00", "2025-02-14 00:00:00", BigDecimal.valueOf(33.3333)),
//                        new StockInOrderDetailParam("子件001", 2, BigDecimal.valueOf(10), BigDecimal.valueOf(20), BigDecimal.valueOf(3), BigDecimal.valueOf(60), "c", "A-03-1", "HAZS2321", "2025-02-14 00:00:00", "2025-02-14 00:00:00", BigDecimal.valueOf(66.6667))))
//        );

//        params.put("stock_in_order_detail_list",
//                JsonUtils.serialize(List.of(new StockInOrderDetailParam("测试13", 1, BigDecimal.valueOf(10), BigDecimal.valueOf(10), BigDecimal.valueOf(10), BigDecimal.valueOf(100), "z", "A-02-2", "HAZS1321", "2025-02-14 11:00:00", "2025-02-14 00:00:00", BigDecimal.valueOf(100))))
//        );

        try {
            String response = client.execute("stockin_order_push.php", params);
            System.out.println(response);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 创建采购入库单
     */
    public static void createPurchaseStockinOrder() throws Exception {
        WdtClient client = new WdtClient(CommonConstants.sid, CommonConstants.appkey, CommonConstants.appSecret, CommonConstants.baseUrl);

        Map<String, String> params = new HashMap<String, String>();
        params.put("warehouse_id", "5");
        params.put("src_order_type", "1");
        params.put("remark", "桀桀桀桀");
        params.put("reason", "想入");
        params.put("is_save", "false");
        params.put("is_split_detail", "true");
        params.put("need_fail_result", "true");
        params.put("post_amount", "100");
        params.put("post_share_type", "1");

        params.put("src_order_id", "2668045199678111748");
        params.put("src_order_no", "CG2502270003");

//        params.put("stock_in_order_detail_list",
//                JsonUtils.serialize(List.of(new StockInOrderDetailParam("新建", 1, BigDecimal.valueOf(1), BigDecimal.valueOf(97), BigDecimal.valueOf(10), BigDecimal.valueOf(10), "z", "A2货位1", "HAZS1321", "2025-02-14 11:00:00", "2025-02-26 00:00:00", BigDecimal.valueOf(100))))
//        );

        try {
            String response = client.execute("stockin_order_push.php", params);
            System.out.println(response);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 创建其他出库单
     */
    public static void createStockoutOrder() throws Exception {
        WdtClient client = new WdtClient(CommonConstants.sid, CommonConstants.appkey, CommonConstants.appSecret, CommonConstants.baseUrl);

        Map<String, String> params = new HashMap<String, String>();
        params.put("remark", "阿飞无法收到（二）");
        params.put("src_order_type", "7");
        params.put("reason", "君君出库");
        params.put("warehouse_id", "22");
        params.put("stockout_flag", "1");
//        params.put("order_detail_list",
//                JsonUtils.serialize(List.of(new StockOutOrderDetailVO("子件001", 1, BigDecimal.valueOf(2), BigDecimal.valueOf(0), BigDecimal.valueOf(10), BigDecimal.valueOf(20), "啊啊服务", "A-feawfewg", "A-02-2")))
//        );

        try {
            String response = client.execute("stockout_order_push.php", params);
            System.out.println(response);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 创建采购退货出库单
     */
    public static void createPurchaseReturnStockoutOrder() throws Exception {
        WdtClient client = new WdtClient(CommonConstants.sid, CommonConstants.appkey, CommonConstants.appSecret, CommonConstants.baseUrl);

        Map<String, String> params = new HashMap<String, String>();
        params.put("remark", "阿飞无法收到（二）");
        params.put("src_order_type", "3");
        params.put("reason", "反反复复");
        params.put("warehouse_id", "5");
        params.put("stockout_flag", "1");

        params.put("src_order_id", "2668057061815746573");
        params.put("src_order_no", "CT2502270001");


//        params.put("order_detail_list",
//                JsonUtils.serialize(List.of(new StockOutOrderDetailVO("新建", 1, BigDecimal.valueOf(2), BigDecimal.valueOf(95), BigDecimal.valueOf(10), BigDecimal.valueOf(20), "啊啊服务", "", "A1货位1")))
//        );

        try {
            String response = client.execute("stockout_order_push.php", params);
            System.out.println(response);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
